from enum import Enum

# 调用栈

class CallStack:
	def __init__(self):
		self._records = []

	#  入栈
	def push(self, ar):
		self._records.append(ar)

	#  出栈
	def pop(self):
		return self._records.pop()

	#  查询栈
	def peek(self):
		return self._records[-1]

	def __str__(self):
		s = '\n'.join(repr(ar) for ar in reversed(self._records))
		s = f'CALL STACK\n{s}\n'
		return s

	def __repr__(self):
		return self.__str__()




class ARType(Enum):
    # 活动记录类别
    PROGRAM   = 'PROGRAM' # 程序
    CALL = 'FUNCTION'

# 活动记录
class ActivationRecord:
    def __init__(self, name, type, nesting_level):
        self.name = name # 活动记录名称
        self.type = type # 活动记录类别
        self.nesting_level = nesting_level # 活动记录级别
        self.members = {}

    def __setitem__(self, key, value):
        self.members[key] = value

    def __getitem__(self, key):
        return self.members[key]

    def get(self, key):
        return self.members.get(key)

    def __str__(self):
        lines = [
            '{level}: {type} {name}'.format(
                level=self.nesting_level,
                type=self.type.value,
                name=self.name,
            )
        ]
        for name, val in self.members.items():
            lines.append(f'   {name:<20}: {val}')

        s = '\n'.join(lines)
        return s

    def __repr__(self):
        return self.__str__()